//入口函数
$(function () {
    //获取用户信息
    getUserInfo();

    //退出点击事件
    let layer = layui.layer;
    $('#btnLogout').on('click', function () {
        //弹出询问框
        layer.confirm('是否确认退出?', { icon: 3, title: '提示' }, function (index) {
            //清空本地token
            localStorage.removeItem('token');
            //页面跳转
            location.href = '/login.html';
            //关闭询问框
            layer.close(index);
        });
    })
});


//获取用户信息的封装函数

function getUserInfo() {
    $.ajax({
        url: '/my/userinfo',
        // headers: {
        //     Authorization: localStorage.getItem("token") || ""
        // },
        success: function (res) {
            if (res.status != 0) {
                return layui.layer.msg(res.message);
            };
            renderAvatar(res.data);
        }
    });
};

function renderAvatar(user) {
    let name = user.nickname || user.username;
    $('#welcome').html('欢迎&emsp;' + name);
    if (user.user_pic != null) {
        $('.layui-nav-img').show().attr('src', user.user_pic);
        $('.text-avatar').hide();
    } else {
        $('.layui-nav-img').hide();
        let text = name[0].toUpperCase();
        $('.text-avatar').show().html(text);
    }
}